package Java9_23;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.*;

public class Products3 {
    public static void xiugai(int it,int st ){
        SAXReader reader=new SAXReader();
        Document doc=null;
        try {
            doc=reader.read(new File("D:\\IdeaProjects\\src\\Java9_23\\products.xml"));
        Element element= (Element) doc.selectSingleNode("/shop/product[itemno="+it+"]");
        if (element==null){
            System.out.println("没有找到");
            return;
        }
        element.selectSingleNode("stockcount").setText(String.valueOf(st));
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("utf-8");
            XMLWriter xw=new XMLWriter(new FileOutputStream("D:\\IdeaProjects\\src\\Java9_23\\products.xml"),format);
            xw.write(doc);
            xw.close();
            System.out.println("成功打印");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    public static void main(String[] args) {
        xiugai(2949,22);
    }
}
